package cryptoprovider

import (
	"crypto/sha256"
	"encoding/hex"
	"hash"

	"strings"
)

const (
	defaultSalt = "ibcbaas"
)

type SHA2 struct {
	salt    []byte
	encoder hash.Hash
}

func NewSHA2(salt []byte) *SHA2 {
	if salt == nil {
		salt = []byte(defaultSalt)
	}
	return &SHA2{
		salt:    salt,
		encoder: sha256.New(),
	}
}

func (ap *SHA2) Encrypt(plain string) string {
	ap.encoder.Write([]byte(plain))
	hashStr := hex.EncodeToString(ap.encoder.Sum(nil))
	return hex.EncodeToString([]byte(hashStr + string(ap.salt)))
}

func (ap *SHA2) Check(originCipher, attempCipher string) bool {
	if strings.EqualFold(originCipher, attempCipher) {
		return true
	}
	return false
}

func (ap *SHA2) Decrypt(cipher string) (string, error) {
	return cipher, nil
}
